package model

import (
	"database/sql"
	"fmt"
	"time"
)

type User struct {
	Id        int64        `gorm:"primaryKey;autoIncrement"`
	UserId    string       `gorm:"size:255;not null;comment:用户的id"`
	Name      string       `gorm:"size:255;not null;uniqueIndex:name_index;comment:The username"`
	Mobile    string       `gorm:"size:255;not null;default:'';uniqueIndex:mobile_index;comment:The mobile phone number"`
	Gender    string       `gorm:"size:10;not null;default:'male';comment:gender,male|female|unknown"`
	Nickname  string       `gorm:"size:255;not null;default:'';comment:The nickname"`
	Type      int64        `gorm:"not null;default:0;comment:用户类型，0：普通，1：vip"`
	CreatedAt time.Time    `gorm:"not null;default:CURRENT_TIMESTAMP;comment:创建时间"`
	UpdatedAt time.Time    `gorm:"not null;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;comment:最后一次修改的时间"`
	DeletedAt sql.NullTime `gorm:"index;comment:软删除的时间"`
}

func (User) TableName() string {
	return "user"

}

func FindByUserIdOrName(userId string, name string) ([]*User, error) {

	if DB == nil {
		return nil, fmt.Errorf("数据库未初始化")
	}

	var user []*User
	result := DB.Model(&User{}).Where("user_id = ?", userId).Or("name = ?", name).Find(&user)

	if result.Error != nil {
		return nil, result.Error
	}

	return user, nil

}
